package com.esunny.online.controller;

import com.esunny.online.domain.dto.ReturnStructure;
import com.esunny.online.domain.entity.Record;
import com.esunny.online.service.RecordService;
import com.esunny.online.util.HostHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * @author hp
 */
@Controller
@RequestMapping("/record")
@Api(value = "播放记录控制器")
public class RecordController {

    @Resource
    RecordService recordService;

    @Resource
    HostHolder hostHolder;

    /**
     * 通过资源id获取视频资源播放记录
     * @param resourceId 视频资源id
     * @return 该视频资源播放记录信息
     */
    @ApiOperation(value = "获取视频播放记录", notes = "根据视频资源id获取视频播放记录")
    @ApiImplicitParam(paramType = "path", name = "resourceId", value = "资源id", required = true, dataType = "int")
    @ApiResponse(code = 400, message = "请求失败", response = ReturnStructure.class)
    @ResponseBody
    @GetMapping("/getVideoRecord/{resourceId}")
    public ReturnStructure getVideoRecord(@PathVariable("resourceId") int resourceId){
        int code;
        String message;
        Map<String, Object> map = new HashMap<>(0);
        String username = hostHolder.getUser().getUsername();
        Record videoRecord = recordService.getVideoRecord(username, resourceId);
        if(videoRecord != null){
            code = 200;
            message = "获取成功";
            map.put("record",videoRecord);
        }else{
            code = 404;
            message = "获取失败";
        }
        return new ReturnStructure(code,message,map);
    }

    /**
     * 根据用户名和资源id更新视频播放记录
     * @param record 要更改的数据
     * @return 数据返回体
     */
    @ApiOperation(value = "更新视频播放记录", notes = "根据用户名和资源id更新视频播放记录")
    @ApiImplicitParam(paramType = "body", name = "record", value = "播放记录", required = true, dataType = "Record")
    @ApiResponse(code = 400, message = "请求失败", response = ReturnStructure.class)
    @ResponseBody
    @PostMapping("/updateVideoRecord")
    public ReturnStructure updateVideoRecord(@RequestBody Record record){
        int code;
        String message;
        Map<String, Object> map = new HashMap<>(0);
        String username = hostHolder.getUser().getUsername();
        record.setUser(username);
        Boolean flag = recordService.updateVideoRecord(record);
        if(flag){
            code = 200;
            message = "更改成功";
            map.put("record",record);
        }else{
            code = 404;
            message = "更改失败";
        }
        return new ReturnStructure(code,message,map);
    }

}
